package com.hspedu.homework.homework3;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/3
 * @time 18:35
 */
public class TCPClientDownload {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文件名:");
        String downloadFileName = scanner.nextLine();
        //创建输出流，将数据冲到管道
        OutputStream outputStream = socket.getOutputStream();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        byte[] data = downloadFileName.getBytes();
        bufferedOutputStream.write(data);
        bufferedOutputStream.flush();
        socket.shutdownOutput();

        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\mytemp\\"+downloadFileName));
        byte[] readBuf = new byte[1024];
        int len = 0;
        while ((len = bis.read(readBuf)) != -1){
            bos.write(readBuf,0,len);
        }
        bos.flush();
//        byte[] bytes = StreamUtils.streamToByteArray(bis);
//        bos.write(bytes);

        socket.close();
    }
}
